<?php 
// Start the session
session_start();

// baza bilan boglanish
include("admin/db.php");

include("lang/uz.php");

/// xatolikni kursatish funk
function myerror($message){
echo ("<p style='color:#CC0000; border:1px solid red; margin:3px; padding:3px; background:#FFDFDF; font-family:Arial;'><b>
	 $message , <br>iltimos, bu haqida adminga xabar qiling: Leonardo73@rambler.ru </b><br>".mysql_error()."</p>");
}
/// xatolikni kursatish funk

/// sayt dizaynini olamiz
$res_design = mysql_query("SELECT name FROM design");
if(@mysql_num_rows($res_design) > 0)
{
$row_design = mysql_fetch_array($res_design);
$design = $row_design['name'];
}
else { $design = 'default'; }
/// sayt dizayni finish

//// vaqt start
function ConvertDate($time_sql) {
	$oylar = array("", "Yanvar", "Fevral", "Mart", "Aprel", "May", "Iyun", "Iyul", "Avgust", "Sentabr", "Oktabr", "Noyabr", "Dekabr");
	$date = strtotime($time_sql);
	$kun = date("j", $date);
	$yil = date("Y", $date);
	$oy = $oylar[date("n", $date)];
	$final_date = " $kun-$oy,  $yil - yil";
	// 
	return $final_date;
}
/// vaqt finish

///// contactni olish
$result_contact = mysql_query("SELECT tel, email, tel2, email2, hamkor FROM contact");
if (!$result_contact)
{ myerror("Contact jadvalidan malumotlar olinmadi, xatolik"); }
$row_contact = mysql_fetch_object($result_contact);
///// contact finish

///// sidebar news start
function sidebar_news() {
	$res_news = mysql_query("SELECT id_news, title_news FROM news ORDER by id_news DESC LIMIT 7");
	if(mysql_num_rows($res_news) > 0)
	{
		$row_news = mysql_fetch_array($res_news);
		do
		{
		printf("<li> <a href=\"news.php?id=%s\"> %s </a> </li>\n", $row_news["id_news"], $row_news["title_news"]);
		}
		while($row_news = mysql_fetch_array($res_news));
	}
	else { echo "<p><br>Hozircha Yangilik yo'q</p>"; }
}
///// sidebar news finish

///// tovar sidebar start
function tovar_sidebar($tip) {

if($tip == "popular") 
{ $query = "SELECT id,title,img_mini FROM data ORDER by view DESC LIMIT 3";  }
else 
if($tip == "latest") 
{ $query = "SELECT id,title,img_mini FROM data ORDER by id DESC LIMIT 3";  }

$result3 = mysql_query($query);
if(mysql_num_rows($result3) > 0)
	{
		$row3 = mysql_fetch_array($result3);
		do
		{
		if($row3["img_mini"] == '') { $row3["img_mini"] = "fotos/nofoto.jpg"; }
		printf("<li> <img src='%s' /> <a href='view_tovar.php?id=%s'>%s</a><br> 
		<a href='cart.php?action=add&id=%s'>".ADD_TO_CART."</a> </li>\n", 
		$row3["img_mini"], $row3["id"], $row3["title"], $row3["id"]);
		}
		while($row3 = mysql_fetch_array($result3));
	}
	else { echo "<p><br>Hozircha tovar yo'q</p>"; }
}
///// tovar sidebar finish

///// menuni olish kodi
function menu($parent) {
	   global $m_a;
	   $h_c = false;
	   foreach ($m_a as $k => $v ) {
	   if ( $v['parent'] == $parent ) {
	   if ( $h_c === false ) {
	   $h_c = true;
	   echo "<ul>";
	   }
	   echo '<li><a href="view_cat.php?cat='.$k.'">'. stripslashes($v['title']).'</a>';

	   menu($k);
	
	   echo "</li>\n";
	   }
	   }
	   if ( $h_c === true ) {
	   echo "</ul>";
	   }
}
////// menu kodi finish

////// tovar olish funksiya
function tovarlar($page) {
global $db;
global $cat;
global $search;

if($page == "index") 
{ $query = "SELECT id,title,img_mini,price FROM data WHERE custom='main' ORDER by id DESC"; $sorry = NO_GOODS; }
else 
if($page == "cat") 
{ $query = "SELECT id,title,img_mini,price FROM data WHERE cat='$cat' ORDER by id DESC";  $sorry = NO_GOODS;}
else
if($page == "recomend") 
{ $query = "SELECT id,title,img_mini,price FROM data WHERE cat='$cat' ORDER BY RAND() limit 3";  $sorry = NO_GOODS;}
else
if($page == "search") 
{ $query = "SELECT id,title,img_mini,price FROM data WHERE title LIKE '%$search%' OR text LIKE '%$search%' ORDER by id DESC"; 
$sorry = "<p>".SORRY."</p>".
	// google dan kursat
	"<p> Lekin, <u>".$search."</u> haqida internetdan ko'p narsa topildi. Ularni ko'rish uchun 
	<a href=\"http://www.google.co.uz/search?q=" . $search . "\" target=\"_blank\">Bu yerni bosing. </a> </p>"; 
}

	$result = mysql_query($query, $db);
	if (!$result)
	{ myerror("Bazadan tovar haqida malumot olinmadi"); }
	if(mysql_num_rows($result) > 0)
	{
/* tovarlarni tsikl bilan chiqar */
	while(list($id, $title, $img_mini, $price) = mysql_fetch_array($result)) {
	
	if($img_mini == '') { $img_mini = "fotos/nofoto.jpg"; }
	echo "<div class='tovar'> 
			<a href='view_tovar.php?id=$id'> 
			<img src='$img_mini' width='160' height='160' /> 
			<p class='tovarname'>$title </p>
			</a>
			<p class='narx'>".PRICE." $price </p>
			<a href='cart.php?id=$id&action=add'><img src='themes/default/img/add.png' alt='Add' title='Add' /></a>
			<a href='view_tovar.php?id=$id#form'> <img src='themes/default/img/buy.png' alt='Buy' title='Buy' /></a> 
		  </div>";
	}
/* tsikl finish */
	}
	else
	{
		// agar tovar bulmasa yoki topilmasa
		echo $sorry;
	}
}
////// tovar olish funksiya finish

////// shopping cart function start
function showCart() {
global $id;
global $db;
	$cart = $_SESSION['cart'];
	if ($cart) {
		$items = explode(',',$cart);
		$contents = array();
		foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}
		$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
		$output[] = '<table class="cart">
		<tr>
			<th>'.GOODS.'</th>
			<th>'.PRICE.'</th>
			<th>'.DONA.'</th>
			<th>'.SUM.'</th>
			<th>'.DELETE.'</th>
		</tr>';
		foreach ($contents as $id=>$qty) {
			$result = mysql_query('SELECT title, price FROM data WHERE id = '.$id);
			$row = mysql_fetch_array($result);
			@ extract($row);
			$output[] = '<tr>';
			$output[] = '<td>'.$title.'</td>';
			$output[] = '<td>'.$price.'</td>';
			$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
			$output[] = '<td>'.($price * $qty).'</td>';
			$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">
			<img src="themes/default/img/del.png" width="16" height="16" align="absmiddle" /> '. DEL .'</a></td>';
			$total += $price * $qty;
			$output[] = '</tr>';
		}
		$output[] = '</table>';
		$output[] = '<button type="submit">'. SAVE .'</button>';
		$output[] = '<div style="font-size:18px; float:right">'. TOTAL . $total. '</div>';
		$output[] = '</form>';
	} else {
		$output[] = '<p>'.EMPTY_CART.'</p>';
	}
	return join('',$output);
}
////// shopping cart function finish


///// sidebar shoppng cart start
function showCart_mini() {
	$cart = $_SESSION['cart'];
	if ($cart) {
		$items = explode(',',$cart);
		$contents = array();
		foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}
		foreach ($contents as $id=>$qty) {
			$result = mysql_query('SELECT title, price, cat FROM data WHERE id = '.$id);
			$row = mysql_fetch_array($result);
			@ extract($row);
			$total += $price * $qty;
		}
		$output[] = '<p>'. TOVAR_COUNT . count($items).' ta.</p>'; 
		$output[] = '<p>'. TOTAL . $total. '</p>';
	} else {
		$output[] = '<p>'.EMPTY_CART.'</p>';
	}
	return join('',$output);
}

//// zakazni saqlash uchun cart
function showCart_save() {
	global $db;
	$cart = $_SESSION['cart'];
	if ($cart) {
		$items = explode(',',$cart);
		$contents = array();
		foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}
		$output[] = "<table border='1' cellpadding='5' width='80%'>
		<tr>
			<th>Tovar</th>
			<th>Narx</th>
			<th>Dona</th>
			<th>Summa</th>
		</tr>";
		foreach ($contents as $id=>$qty) {
			$result = mysql_query('SELECT title, price FROM data WHERE id = '.$id);
			$row = mysql_fetch_array($result);
			@ extract($row);
			$output[] = '<tr>';
			$output[] = '<td>'.$title.'</td>';
			$output[] = '<td>'.$price.'</td>';
			$output[] = '<td>'.$qty.'</td>';
			$output[] = '<td>'.($price * $qty).'</td>';
			$total += $price * $qty;
			$output[] = '</tr>';
		}
		$output[] = '</table>';
		$output[] = '<p>'. TOTAL . $total. '</p>';
	} else {
		$output[] = '<p>'.EMPTY_CART.'</p>';
	}
	return join('',$output);
}
//// zakazni saqlash uchun cart finish

/*  #######  sidebar shopping cart start ######

//// sidebar cart minimum malumot kursatish

function showCart_mini2() {
	$cart = $_SESSION['cart'];
	if (!$cart) {
		return '<p>'.NOTHING.'</p>';
	} else {
		// Parse the cart session variable
		$items = explode(',',$cart);
		return '<p>Tovar: <a href="cart.php">'.count($items).' ta.</a></p>
		<p> </p>';
	}
}

/// sidebar qismdagi savatni batafsilroq kurinishda chiqarish /////

function showCart_mini3() {
	$cart = $_SESSION['cart'];
	if ($cart) {
		$items = explode(',',$cart);
		$contents = array();
		foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}
		$output[] = '<table class="cart_mini">
		<tr>
			<th>'.GOODS.'</th>
			<th>'.DONA.'</th>

		</tr>';
		foreach ($contents as $id=>$qty) {
			$result = mysql_query('SELECT title, price, cat FROM data WHERE id = '.$id);
			$row = mysql_fetch_array($result);
			@ extract($row);
			$output[] = '<tr>';
			$output[] = '<td><a href=view_tovar.php?id='.$id.'>'.$title.'</a></td>';
			$output[] = '<td>'.$qty.' ta </td>';
			$total += $price * $qty;
			$output[] = '</tr>';
		}
		$output[] = '</table>';
		$output[] = '<p>'. TOVAR_COUNT . count($items).' ta.</p>'; 
		$output[] = '<p>'. TOTAL . $total. '</p>';
	} else {
		$output[] = '<p>'.EMPTY_CART.'</p>';
	}
	return join('',$output);
}

////////////////////////////// */
///// ######  sidebar shoppng cart finish ###### ///
?>